/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.business.dao;

import han.bifang.common.entity.SoaWsdl;
import han.bifang.common.exception.BifangErrorCode;
import han.bifang.common.exception.BifangException;
import han.bifang.common.persist.dao.DaoManager;
import han.bifang.common.persist.tx.Tx;
import han.bifang.common.persist.tx.TxManager;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * @Author Kent
 */
public class WsdlDao {
	
	public static void createWsdl(SoaWsdl d) throws BifangException {
		Tx tx = null;
		try {
			tx = TxManager.start();
			DaoManager.getDao().save(d);
			DaoManager.getDao().flush();
			tx.commit();
		} catch (Throwable e) {
			Log logger = LogFactory.getLog(WsdlDao.class);
			logger.error("WsdlDao.createWsdl()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"createWsdl() error", e);
		} finally {
			tx.end();
		}

	}
	
	public static String getWsdl(String id) throws BifangException {
		Tx tx = null;
		try {
			tx = TxManager.start();
			SoaWsdl d = (SoaWsdl)DaoManager.getDao().get(SoaWsdl.class,id);
			
			tx.commit();
			return d.getWsdl();
		} catch (Throwable e) {
			Log logger = LogFactory.getLog(WsdlDao.class);
			logger.error("WsdlDao.getWsdl()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"getWsdl() error", e);
		} finally {
			tx.end();
		}

	}
	
	public static SoaWsdl findWsdlByVersionId(String id) throws BifangException {
		Tx tx = null;
		try {
			tx = TxManager.start();
			String hql = "from SoaWsdl s where s.versionId = ?";
			List l = DaoManager.getDao().find(hql,id);
			SoaWsdl d = (SoaWsdl)l.get(0);
			
			tx.commit();
			return d;
		} catch (Throwable e) {
			Log logger = LogFactory.getLog(WsdlDao.class);
			logger.error("WsdlDao.findWsdlByVersionId()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"findWsdlByVersionId() error", e);
		} finally {
			tx.end();
		}

	}
	
	public static void removeWsdlByVersionId(String id) throws BifangException {
		Tx tx = null;
		try {
			tx = TxManager.start();
			String hql = "from SoaWsdl s where s.versionId = ?";
			List l = DaoManager.getDao().find(hql,id);
			DaoManager.getDao().deleteAll(l);
			DaoManager.getDao().flush();
			tx.commit();
			return ;
		} catch (Throwable e) {
			Log logger = LogFactory.getLog(WsdlDao.class);
			logger.error("WsdlDao.removeWsdlByVersionId()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"removeWsdlByVersionId() error", e);
		} finally {
			tx.end();
		}

	}
	
	public static void removeWsdl(String id) throws BifangException {
		Tx tx = null;
		try {
			tx = TxManager.start();
			SoaWsdl d = (SoaWsdl)DaoManager.getDao().get(SoaWsdl.class,id);
			DaoManager.getDao().delete(d);
			tx.commit();
			
		} catch (Throwable e) {
			Log logger = LogFactory.getLog(WsdlDao.class);
			logger.error("WsdlDao.removeWsdl()", e);
			tx.rollback();
			throw new BifangException(BifangErrorCode.DB_ERROR,
					"removeWsdl() error", e);
		} finally {
			tx.end();
		}

	}

}
